<?php
namespace Admin\Controller;
class LinkController extends CommonController{
    protected function _search() {
        $map = array();
        ($cate_id = I('get.cate_id')) && $map['cate_id'] = array('eq', $cate_id);
        ($keyword = I('get.keyword')) && $map['name'] = array('like', '%'.$keyword.'%');
        $this->assign('search', array(
            'keyword' => $keyword,
            'cate_id' => $cate_id,
        ));
        return $map;
    }

    public function _before_index() {
        $big_menu = array(
            'title' => '增加链接',
            'iframe' => U('Link/add'),
            'id' => 'add',
            'width' => '500',
            'height' => '200'
        );
        $this->assign('big_menu', $big_menu);
        $this->list_relation = true;
        $this->_before_add();
        $this->_ajaxedit = true;
        //默认排序
        $this->sort = 'ordid';
        $this->order = 'ASC';
    }

    public function _before_insert($data){
        //$data['url']=array_pop(explode('//',$_POST['url']));
        //return $data;
    }

    public function _before_add() {
        $cate_list = D('LinkCate')->where(array('status'=>1))->select();
        $this->assign('cate_list',$cate_list);
    }

    public function _before_edit(){
        $this->_before_add();
    }

    public function _before_update($data){
        //$data['url']=array_pop(explode('//',$_POST['url']));
        //return $data;
    }

    //检查链接名称是否存在
    public function ajax_check_name(){
        $name = I('name');
        $id = I('id');
        if (D('Link')->name_exists($name, $id)) {
            $this->ajaxReturn(0, '链接名称已经存在');
        } else {
            $this->ajaxReturn();
        }
    }

    public function ajax_upload_img() {
        //上传图片
        if (!empty($_FILES['img']['name'])) {
            //remove_origin'=>true替换掉原图
            $result = $this->_upload($_FILES['img'], './Uploads/link/');
            if ($result['error']) {
                $this->ajaxReturn(0, $result['info']);
            } else {
                $ext = array_pop(explode('.', $result['info'][0]['savename']));
                $data['img'] = $result['info'][0]['savename'];
                $this->ajaxReturn(1, L('operation_success'), $data['img']);
            }
        } else {
            $this->ajaxReturn(0, L('illegal_parameters'));
        }
    }


}